<?php

declare (strict_types = 1);

namespace app\api\controller;

use app\api\service\JwtService;
use app\common\model\tang\MerchantLog;
use think\App;
use think\facade\Config;
use think\facade\Request;
use think\Response;
use think\Validate;
use think\exception\HttpResponseException;

/**
 * 控制器基础类
 */
abstract class Base
{
    public $mobile;
    public $openid;
    public $user_id;
    public $merchant_id = 1;
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    /**
     * 检测是否登录
     * @var array
     */
    protected $middleware = [
//        'app\api\middleware\CheckApi'=>['except' => ['login', 'register','test']],
    ];

    /**
     * 分页数量
     * @var string
     */
    protected $pageSize = '';

    public $keli_sdk;

    /**
     * 构造方法
     * @access public
     * @param  App $app 应用对象
     */
    public function __construct(App $app)
    {
        $this->app = $app;
        $this->request = $this->app->request;
        $this->openid = $this->request->openid;
        $this->mobile = $this->request->mobile;
        $this->user_id = $this->request->user_id;
        $this->merchant_id = $this->request->user_id;
        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {
        $configs = [
            'appId' => 'keli_mebaby',
            'appSecret' => '123456',
            'debug'  => false
        ];

//获取sdk实例
        $dispatch = new \Keli\OpenSDK\Dispatch($configs);
        $this->keli_sdk = $dispatch;

    }
    /**
     * @return \think\response\Json 静默授权
     */
    public function auth()
    {
        $code = $this->request->param('code');
        $getUserInfo = $this->app->openPlatform->auth->session($code); //返回openid 和 sessionkey
        if (!isset($getUserInfo['session_key']) || empty($getUserInfo['session_key'])) {
            return SendError("code不不正确" . 'session_key:' . $getUserInfo['session_key']);
        }
        $data['username'] = '';
        $data['password'] = '';
        $merchantLog = (new MerchantLog())->where('openid',$getUserInfo['openid'])->find();
        if($merchantLog){
            $data['username'] = $merchantLog['username'];
            $data['password'] = $merchantLog['password'];
        }
        $data['openid'] = $getUserInfo['openid'];
        return SendSuccess("静默授权成功", $data);
    }

    /**
     * @return mixed
     * @author: Hhy <jackhhy520@qq.com>
     * @describe:获取用户id
     */
    protected function getUid(){
        $jwtAuth = JwtService::getInstance();
        return $jwtAuth->getUid();
    }

    /**
     * @return mixed
     * @author: Hhy <jackhhy520@qq.com>
     * @describe:获取用户手机号码
     */
    protected function getMobile(){
        $jwtAuth = JwtService::getInstance();
        return $jwtAuth->getMobile();
    }


}
